package br.com.lh.urabuilder.domain.xml
{
	import br.com.lh.urabuilder.util.ValidatorFactory;
	import br.com.lh.urabuilder.util.XMLUtils;
	import flash.utils.describeType;
	
	import mx.collections.XMLListCollection;
	
	public class BaseAction implements UraAction{
		public var id:int;
		
		public var logMessage:UraActionField =
		new UraActionField(ValidatorFactory.getValidator(ValidatorFactory.STRING_VALIDATOR,30,false));
		
		public function BaseAction(){
		}
		
		
		public function setId(id:int):void{
			this.id = id;
		}
		
		//Abstrata???
		public function toXML(includeNull:Boolean):XML{
			return null;
		}
		
		public function buildFromXml(element:XML):void{
			var x:XML = describeType(this);
			var variables:XMLList = x..variable;
			for each(var n:XML in variables){
				var nodeName:String =n.@name.toString();
				var xmlValue:String = element['@' + nodeName];
				if (this[nodeName] is UraActionField && xmlValue !=""){
					UraActionField(this[nodeName]).value = xmlValue;
				}
				
			}
		}
		
		public function xmlName():String{
			return null;
		}
		
		public function toolTipText():String{
			return null;
		}
		
		public function isLinked():Boolean{
			return true;
		}
		
		protected function buildXml(node:XML,includeNull:Boolean):void{
			var x:XML = describeType(this);
			var variables:XMLList = x..variable;
			var coll:XMLListCollection = new XMLListCollection(variables);
			XMLUtils.sortByName(coll);
			for each(var n:XML in coll){
				var nodeName:String =n.@name.toString();
				if (this[nodeName] is UraActionField){
					var value:Object = UraActionField(this[nodeName]).value;
					if (value!= null || includeNull){
						node['@' + nodeName] = value;
					}
				}else if (nodeName.indexOf("nextId") == -1 || this[nodeName] != -1){
					node['@' + nodeName] = this[nodeName];
				}
				
			}
		}
		
		public function getVariables():XMLList{
			var x:XML = describeType(this);
			var xl:XMLList = x..variable;
			return xl;
		}
		
		
		public function getId():int{
			return id;
		}
		
	}
}